這篇是要延續第13天
把Spring Boot從JPA延續到REST APIs
要建立一些packagec和一些java檔..
然後一些java檔是做interface
變數的意思(取名字是很重要的)
id 排第幾個
sku 通常表示規格、顏色、款式
name ,名字
description描述
unit_price 價格
image_url 照片放的位置
active 這邊基本上都打1還在想如何解釋
units_in_stock 庫存數
date_created 被創建的日期
last_updated 最後更新時間
import lombok.Data;
import org.springframework.data.annotation.Id;
import javax.persistence.*;
import java.math.BigDecimal;
import java.util.Date;
@Entity
@Table(name="product")
@Data
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
private String sku;
private String name;
private String description;
private BigDecimal unitPrice;
private String imageUrl;
private boolean active;
private int unitsInStock;
private Date dateCreated;
private Date lastUpdated;
}
其中@Table(name="product")的product如果反紅代表沒有連到資料庫
然後其他地方反紅就是要按"alt+enter"讓他import
在每個變數上都加上 @Column(name = "變數")這個變數是對應資料庫的命名
import lombok.Data;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;
import javax.persistence.*;
import java.math.BigDecimal;
import java.util.Date;
@Entity
@Table(name="product")
@Data
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@Column(name = "sku")
private String sku;
@Column(name = "name")
private String name;
@Column(name = "description")
private String description;
@Column(name = "unit_price")
private BigDecimal unitPrice;
@Column(name = "image_url")
private String imageUrl;
@Column(name = "active")
private boolean active;
@Column(name = "units_in_stock")
private int unitsInStock;
@Column(name = "date_created")
@CreationTimestamp
private Date dateCreated;
@Column(name = "last_updated")
@UpdateTimestamp
private Date lastUpdated;
}
然後跟日期date有關的地方還要特別加上
@CreationTimestamp
private Date dateCreated;
@Column(name = "last_updated")
@UpdateTimestamp
private Date lastUpdated;
在entity package的下面再建一個ProductCategory
@Entity
@Table(name="productcategory")
都KEY上但是到data的地方就不同了
import lombok.Getter;
import lombok.Setter;
import javax.persistence.Entity;
import javax.persistence.Table;
import java.util.Set;
@Entity
@Table(name="product_category")
@Getter
@Setter
public class ProductCategory {
private Long id;
private String categoryName;
private Set<Product> prodict;
}
把它完成好準備建立dao進入REST APIs
import lombok.Getter;
import lombok.Setter;
import javax.persistence.*;
import java.util.Set;
@Entity
@Table(name="product_category")
@Getter
@Setter
public class ProductCategory {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@Column(name = "category_name")
private String categoryName;
@OneToMany(cascade = CascadeType.ALL,mappedBy = "category")
private Set<Product> products;
}
@OneToMany的意思:一對多
這裡的 @OneToMany(cascade = CascadeType.ALL,mappedBy = "category")會反紅
所以~
再回到Product.java檔新增
@JoinColumn(name = "category_id",nullable = false)
private ProductCategory category;
完成JPA~
import lombok.Data;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;
import javax.persistence.*;
import java.math.BigDecimal;
import java.util.Date;
@Entity
@Table(name="product")
@Data
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@ManyToOne
@JoinColumn(name = "category_id",nullable = false)
private ProductCategory category;
@Column(name = "sku")
private String sku;
@Column(name = "name")
private String name;
@Column(name = "description")
private String description;
@Column(name = "unit_price")
private BigDecimal unitPrice;
@Column(name = "image_url")
private String imageUrl;
@Column(name = "active")
private boolean active;
@Column(name = "units_in_stock")
private int unitsInStock;
@Column(name = "date_created")
@CreationTimestamp
private Date dateCreated;
@Column(name = "last_updated")
@UpdateTimestamp
private Date lastUpdated;
}
因為晚上會晚點回家~
所以我先發表~
DEAR ALL 我們明天見~